package com.javacv4axe.video;

import org.axe.util.LogUtil;

import com.javacv4axe.core.CammeraWatcher;
import com.javacv4axe.core.FramePack;

/**
 * Http视频播放Handler
 * 自动开关摄像头
 */
public class AutoHttpVideoHandler extends HttpVideoHandler{
	
	public AutoHttpVideoHandler() {
		super();
	}
	
	public AutoHttpVideoHandler(int windowWidth,int windowHeight){
		super(windowWidth, windowHeight);
	}
	
	@Override
	public void cacheFrame(FramePack framePack) {
		super.cacheFrame(framePack);
		if(getSocketSet().size() <= 0){
			getCammeraWatcher().stopWatch();
			if(debug()){
				LogUtil.log("停止拍照");
			}
		}
	};
	public void listenOnNewConnection() {
		if(getSocketSet().size() >0){
			getCammeraWatcher().startWatch(cammeraIndexStart());
			if(debug()){
				LogUtil.log("开始拍照");
			}
		}
	};
	
	@Override
	public void setCammeraWatcher(CammeraWatcher cammeraWatcher) {
		super.setCammeraWatcher(cammeraWatcher);
		
		//先行开启摄像头，并关闭摄像头一次，加快后续速度
		cammeraWatcher.startWatch(cammeraIndexStart());
		cammeraWatcher.stopWatch();
	}
	
	public int cammeraIndexStart(){
		//留给实现者可以自己继承并定义
		return 0;//默认从0开始
	}
}
